package com.zzy.springbootdemo.design_01_factory.simple;

import com.zzy.springbootdemo.design_01_factory.simple.service.RewardService;
import com.zzy.springbootdemo.design_01_factory.simple.service.impl.ECardRewardService;
import com.zzy.springbootdemo.design_01_factory.simple.service.impl.MaterialRewardService;
import com.zzy.springbootdemo.design_01_factory.simple.service.impl.VirtualRewardService;

public class SimpleFactory {

    /**
     * 通过奖励类型返回奖励服务类
     * @param rewordType 奖励类型
     * @return
     */
    public RewardService getRewordServiceByType(Integer rewordType){
        if(rewordType == null){
            return null;
        }
        if(rewordType == 1){
            return new ECardRewardService();
        }
        if(rewordType == 2){
            return new MaterialRewardService();
        }
        if(rewordType == 3){
            return new VirtualRewardService();
        }
        throw new RuntimeException("不存在的奖励类型");
    }


    public RewardService getRewordServiceByClazz(Class<? extends RewardService> clazz){
        if (clazz == null){
            return null;
        }
        try {
            return clazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

}
